package com.karry.yl.web.baseinfo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.baseinfo.*;
import com.karry.yl.service.baseinfo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 床位信息Controller
 *
 */
@Controller
@RequestMapping("/bed")
public class BedController extends SuperController {
    @Autowired
    private BedService bedService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private BedTypeService bedTypeService;
    @Autowired
    private OrganizationService organizationService;

    @RequestMapping
    public String listUI(ModelMap map){
        List<Building> buildings = buildingService.selectList(new EntityWrapper<Building>().eq("orgId", currentUser.getOrgId()).eq("deleted", false));
        map.put("buildings",buildings);
        Organization organization = organizationService.selectById(currentUser.getOrgId());
        Integer userType = organization.getNature();
        map.put("userType", userType);
        return "baseinfo/bed/list";
    }

    @RequestMapping("/add")
    public String addUI(ModelMap map){
        List<Building> buildings = buildingService.selectList(new EntityWrapper<Building>().eq("orgId", currentUser.getOrgId()));
        map.put("buildings",buildings);
        List<BedType> bedTypes=bedTypeService.selectList(new EntityWrapper<BedType>().eq("orgId", currentUser.getOrgId()));
        map.put("bedTypes",bedTypes);
        Organization organization = organizationService.selectById(currentUser.getOrgId());
        Integer userType = organization.getNature();
        map.put("userType", userType);
        return "baseinfo/bed/add";
    }

    @RequestMapping("edit")
    public String editUI(Long id, ModelMap map){
        Bed bed = bedService.selectById(id);
        map.put("bed",bed);
        List<Building> buildings = buildingService.selectList(new EntityWrapper<Building>().eq("orgId", currentUser.getOrgId()).eq("deleted", false));
        map.put("buildings",buildings);
        List<Room> rooms = roomService.selectList(new EntityWrapper<Room>().eq("orgId", currentUser.getOrgId()).eq( "buildingId", bed.getBuildingId()).eq("deleted",false));
        map.put("rooms",rooms);
        List<BedType> bedTypes=bedTypeService.selectList(new EntityWrapper<BedType>().eq("orgId", currentUser.getOrgId()));
        map.put("bedTypes",bedTypes);
        Organization organization = organizationService.selectById(currentUser.getOrgId());
        Integer userType = organization.getNature();
        map.put("userType", userType);
        return "baseinfo/bed/edit";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit, String buildingId, String floor,String roomId){
        long buildingIdNum=0;
        long roomIdNum=0;
        if(!StringUtils.isEmpty(buildingId)){
            buildingIdNum=Long.parseLong(buildingId);
        }
        if(!StringUtils.isEmpty(roomId)){
            roomIdNum=Long.parseLong(roomId);
        }
        Page<Bed> roomPage = bedService.selectBedPage(new Page<Bed>(page, limit),currentUser.getOrgId(),buildingIdNum,floor,roomIdNum);
        return new LayTableResponse(roomPage.getRecords(),roomPage.getTotal());
    }

    @RequestMapping("/save")
    public String save(Bed bed){
        if(bed.getTypeId() != null){
            BedType bedType=bedTypeService.selectById(bed.getTypeId());
            bed.setTypeName(bedType.getName());
            bed.setPrice(bedType.getPrice());
        }
        bed.setOrgId(currentUser.getOrgId());
        bedService.save(bed);
        return "redirect:/bed";
    }

    @RequestMapping("/update")
    public String update(Bed bed){
        if(bed.getTypeId() != null){
            BedType bedType=bedTypeService.selectById(bed.getTypeId());
            bed.setTypeName(bedType.getName());
            bed.setPrice(bedType.getPrice());
        }
        bed.setOrgId(currentUser.getOrgId());
        bedService.update(bed);
        return "redirect:/bed";
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        bedService.delete(ids);
        return "redirect:/bed";
    }

    @RequestMapping("/rooms")
    @ResponseBody
    public JsonResponse getRoomByBuildingId(Long buildingId,int floor){
        List<Room> rooms = roomService.selectList(new EntityWrapper<Room>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("buildingId", buildingId).eq("floor",floor));
        return JsonResponse.success("rooms",rooms);
    }

    @RequestMapping("/saveList")
    @ResponseBody
    public JsonResponse saveList(String floor,String code[],Long[] typeId,String type[],String roomId,String buildingId) {
        Long orgId=currentUser.getOrgId();
        bedService.saveLsit(floor, code,typeId,type,roomId,buildingId,orgId);
        return  JsonResponse.success();
    }
    @RequestMapping("/selecBedtList")
    @ResponseBody
    public JsonResponse selecBedtList(Long buildingId,Long roomId,Long id) {
        List<Bed> beds =null;
        if(buildingId !=null){
            beds = bedService.selectList(new EntityWrapper<Bed>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("buildingId",buildingId));
        }
        if(roomId !=null){
            beds = bedService.selectList(new EntityWrapper<Bed>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("roomId",roomId));
        }
        if(id !=null){
            beds = bedService.selectList(new EntityWrapper<Bed>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("id",id));
        }
        for(Bed b:beds){
            if(b.getStatus()==2){
                return JsonResponse.fail("err");
            }
        }
        return JsonResponse.success(beds);
    }
    @RequestMapping("/selecBedts")
    @ResponseBody
    public JsonResponse selecBedts(Long roomId) {
        List<Bed> beds = bedService.selectList(new EntityWrapper<Bed>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("roomId",roomId));
        return JsonResponse.success(beds);
    }
    /*
     * 查询房号是否重复
     * **/
    @RequestMapping("/selectDistinct")
    @ResponseBody
    public JsonResponse selectDistinct(Long budid,Long roomId,String floor,String code) {
        Bed bed=bedService.selectOne(new EntityWrapper<Bed>().eq("orgId",currentUser.getOrgId()).eq("buildingId",budid).eq("floor",floor).eq("roomId",roomId).eq("code",code));
        return JsonResponse.success(bed);
    }
}
